import { _decorator, Component } from 'cc';
const { ccclass, property } = _decorator;

/**
 * 技能基类
 * 
 * 功能：
 * 1. 定义技能的基本属性和行为
 * 2. 管理技能的冷却和消耗
 * 3. 处理技能的生命周期
 * 4. 提供技能效果接口
 * 
 * 特点：
 * 1. 灵活的技能配置
 * 2. 支持技能组合
 * 3. 可扩展的效果系统
 * 4. 技能状态管理
 */
@ccclass('BaseSkill')
export abstract class BaseSkill extends Component {
    @property
    public cooldown: number = 1;

    @property
    public duration: number = 0;

    protected isReady: boolean = true;
    protected isActive: boolean = false;
    protected cooldownTimer: number = 0;
    protected durationTimer: number = 0;

    update(deltaTime: number) {
        this.updateCooldown(deltaTime);
        this.updateDuration(deltaTime);
    }

    private updateCooldown(deltaTime: number) {
        if (!this.isReady) {
            this.cooldownTimer += deltaTime;
            if (this.cooldownTimer >= this.cooldown) {
                this.isReady = true;
                this.cooldownTimer = 0;
            }
        }
    }

    private updateDuration(deltaTime: number) {
        if (this.isActive) {
            this.durationTimer += deltaTime;
            if (this.durationTimer >= this.duration) {
                this.deactivate();
            }
        }
    }

    public canUse(): boolean {
        return this.isReady && !this.isActive;
    }

    public use(): boolean {
        if (!this.canUse()) return false;

        this.isReady = false;
        this.isActive = true;
        this.durationTimer = 0;
        this.onActivate();
        return true;
    }

    protected deactivate() {
        this.isActive = false;
        this.durationTimer = 0;
        this.onDeactivate();
    }

    public getCooldownProgress(): number {
        return this.isReady ? 1 : this.cooldownTimer / this.cooldown;
    }

    protected abstract onActivate(): void;
    protected abstract onDeactivate(): void;
} 